package com.dq.service.org.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgPositionQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.org.centre.OrgDeptPositionCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgPositionService;
import com.dq.service.sys.user.SysUserService;
import com.dq.utils.TreeBuilder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 *
 * 岗位逻辑
 */
@Service
public class OrgPositionServiceImpl  extends BaseRepositories implements OrgPositionService {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 添加
     * @param orgPosition
     * @return
     * @throws AdminException
     */
    @Override
    public OrgPosition save(OrgPosition orgPosition)  throws AdminException {
        orgPosition.setVersion(Constant.VERSION_VALUE);
        orgPosition.setDeleted(false);
        orgPosition.setCreateTime(System.currentTimeMillis());
        orgPosition.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        orgPosition.setCreateUser(user);
        orgPosition.setUpdateUser(user);

        if(orgPosition.getSortNo() == null) {
            orgPosition.setSortNo(1);
        }

        /// 岗位编号
        orgPosition.setOrdersNo(OrgPosition.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));


        OrgPosition flush = orgPositionRepository.saveAndFlush(orgPosition);
        return flush;
    }

    /**
     *修改
     * @param orgPosition
     * @return
     * @throws AdminException
     */
    @Override
    public OrgPosition update(OrgPosition orgPosition) throws AdminException {
        orgPosition.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            orgPosition.setUpdateUser(user);
        }

        OrgPosition flush = orgPositionRepository.saveAndFlush(orgPosition);
        return flush;
    }

    /**
     * 获取所有岗位， 或根据门店获取岗位
     * @param orgPositionQuery
     * @return
     * @throws AdminException
     */
    @Override
    public Vo listByPage(OrgPositionQuery orgPositionQuery) throws AdminException {
        orgPositionQuery.format();
        Map<String, Object> query = orgPositionQuery.parseMap();
        // 去除分页
        query.remove("page");
        query.remove("size");

        List<OrgPosition> orgPositionList;
        Long count;
        if(orgPositionQuery.getOrgId() == null) {
            if (StringUtils.isNotBlank(orgPositionQuery.getPostName())) {
                query.put("postNameLike", BaseAdminQuery.likeSql(orgPositionQuery.getPostName()));
                query.remove("postName");
            }

            orgPositionList = dao.find(dao.query2Hql(OrgPosition.class, query), query);
            ///count = dao.count(dao.count2Hql(OrgPosition.class, query), query);
        }else {
            Org org = dao.get(Org.class, orgPositionQuery.getOrgId());
            if (org == null) {
                throw new AdminException("无对应门店");
            }
            orgPositionQuery.setSection(org);

            if (StringUtils.isNotBlank(orgPositionQuery.getPostName())) {
                query.put("orgPosition_postNameLike", BaseAdminQuery.likeSql(orgPositionQuery.getPostName()));
                query.remove("postName");
            }

            List<OrgDeptPositionCentre> list = dao.find(dao.query2Hql(OrgDeptPositionCentre.class, query), query);
            ArrayList<OrgPosition> arrayList = new ArrayList<>();
            list.forEach(orgDeptPositionCentre -> {
                arrayList.add(orgDeptPositionCentre.getOrgPosition());
            });
            orgPositionList = arrayList;
            ///count = dao.count(dao.count2Hql(OrgDeptPositionCentre.class, query), query);
        }
        // tree树
        List<OrgPosition.Vo> vos = TreeBuilder.orgPositionBuildTree(new HashSet<>((List<OrgPosition.Vo>) VO.toVOCollection(orgPositionList)));
        PageHelper.startPage(orgPositionQuery.getPage(), orgPositionQuery.getSize());
        PageInfo pageInfo = new PageInfo(vos);
        pageInfo.setTotal(vos.size());

        return new Vo(pageInfo);
    }

}

